package com.hxht.cmp.module;

import com.hxht.cmp.entity.User;
import com.hxht.cmp.service.CasePlanService;
import com.hxht.cmp.service.CaseReviewService;
import com.hxht.cmp.service.StatisticalService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author alice on 2017/7/14 0014.
 * @version 1.0
 * @since 1.0
 */
@Controller
public class IndexModule {
    @Autowired
    private CasePlanService casePlanService;
    @Autowired
    private CaseReviewService caseReviewService;
    @Autowired
    private StatisticalService statisticalService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(String courtUid) {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        if (ObjectUtils.isEmpty(courtUid)) {
            User user = (User) session.getAttribute("user");
            courtUid = user.getCourtUid();
        }
        ModelAndView modelAndView = new ModelAndView();
        LocalDate today = LocalDate.now();
        modelAndView.addObject("courtUid", courtUid);
        //今日开庭
        List<Map<String, Object>> todayCase = casePlanService.selectListCasePlan(courtUid, today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "1");
        modelAndView.addObject("todayCase", todayCase);
        //明日
        LocalDate tomorrow = today.plusDays(1L);
        List<Map<String, Object>> tomorrowCase = casePlanService.selectListCasePlan(courtUid, tomorrow.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "1");
        modelAndView.addObject("tomorrowCase", tomorrowCase);
        //回顾
        List<Map<String, Object>> reviewCase = caseReviewService.getReviewIndex(courtUid, "5", "1");
        modelAndView.addObject("reviewCase", reviewCase);

        //顶部统计信息 selectIndexTopStatistical
        HashMap<String, Integer> map = new HashMap<>();
        //全省法庭数量、科技法庭数、平台接入数
        Map<String, Integer> countMap = statisticalService.getCountCourtMap(courtUid);
        //获取正在开庭数
//        int countCourtNowTotal = statisticalService.getCountCourtByStatus(courtUid, 1, today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        int countCourtNowTotal = casePlanService.getOpenNow(courtUid, today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "1");
        //统计当前法院及其下级法院的排期数量
        int countPlan = statisticalService.getCountCourtByStatus(courtUid, 0, null);
        //开庭总数
        int countOpenCourtNum = statisticalService.getCountOpenCourtNum(courtUid);
        //开庭总时长
        int countOpenCourtTime = statisticalService.getCountOpenCourtTime(courtUid);
        //总容量
        int countFileSize = statisticalService.getCountFileSize(courtUid);
        map.put("courtNum", countMap.get("allCourtNum"));
        map.put("technologyCourtTotal", countMap.get("technicalCourtNum"));
        map.put("countCourtNowTotal", countCourtNowTotal);
        map.put("countPlan", countPlan);
        map.put("countOpenCourtNum", countOpenCourtNum);
        map.put("countOpenCourtTime", countOpenCourtTime);
        map.put("countFileSize", countFileSize);
        map.put("thirdCourtNum", countMap.get("thirdCourtNum"));
        modelAndView.addObject("totalMap", map);

        List<Map<String, Object>> timeAndNumList = statisticalService.selectListJudgeOpenTimeAndOpenNum(courtUid, tomorrow.format(DateTimeFormatter.ofPattern("yyyy")) + "-01-01", tomorrow.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        modelAndView.addObject("timeAndNumList", timeAndNumList);
        modelAndView.setViewName(session.getAttribute("indexUrl").toString());
        return modelAndView;
    }

    /**
     * 首页/单独法院
     *
     * @param courtUid
     * @return
     */
    @RequestMapping(value = "/local_index")
    public
    @ResponseBody
    ModelAndView locationIndex(String courtUid) {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        if (ObjectUtils.isEmpty(courtUid)) {
            User user = (User) session.getAttribute("user");
            courtUid = user.getCourtUid();
        }
        ModelAndView modelAndView = new ModelAndView();
        LocalDate today = LocalDate.now();
        modelAndView.addObject("courtUid", courtUid);
        //今日开庭
        List<Map<String, Object>> todayCase = casePlanService.selectListCasePlan(courtUid, today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "0");
        modelAndView.addObject("todayCase", todayCase);
        //明日
        LocalDate tomorrow = today.plusDays(1L);
        List<Map<String, Object>> tomorrowCase = casePlanService.selectListCasePlan(courtUid, tomorrow.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")), "0");
        modelAndView.addObject("tomorrowCase", tomorrowCase);
        //回顾
        List<Map<String, Object>> reviewCase = caseReviewService.getReviewIndex(courtUid, "6", "0");
        modelAndView.addObject("reviewCase", reviewCase);

        //顶部统计信息 selectIndexTopStatistical
        HashMap<String, Integer> map = new HashMap<>();
        //全省法庭数量、科技法庭数、平台接入数
        Map<String, Integer> countMap = statisticalService.getCountCourtMap(courtUid);
        //获取正在开庭数
        int countCourtNowTotal = statisticalService.getCountCourtByStatus(courtUid, 1, today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //统计当前法院及其下级法院的排期数量
        int countPlan = statisticalService.getCountCourtByStatus(courtUid, 0, null);
        //开庭总数
        int countOpenCourtNum = statisticalService.getCountOpenCourtNum(courtUid);
        //开庭总时长
        int countOpenCourtTime = statisticalService.getCountOpenCourtTime(courtUid);
        //总容量
        int countFileSize = statisticalService.getCountFileSize(courtUid);
        map.put("courtNum", countMap.get("allCourtNum"));
        map.put("technologyCourtTotal", countMap.get("technicalCourtNum"));
        map.put("countCourtNowTotal", countCourtNowTotal);
        map.put("countPlan", countPlan);
        map.put("countOpenCourtNum", countOpenCourtNum);
        map.put("countOpenCourtTime", countOpenCourtTime);
        map.put("countFileSize", countFileSize);
        map.put("thirdCourtNum", countMap.get("thirdCourtNum"));
        modelAndView.addObject("totalMap", map);

        List<Map<String, Object>> timeAndNumList = statisticalService.selectListJudgeOpenTimeAndOpenNum(courtUid, tomorrow.format(DateTimeFormatter.ofPattern("yyyy")) + "-01-01", tomorrow.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        modelAndView.addObject("timeAndNumList", timeAndNumList);
        modelAndView.setViewName("local_index");
        return modelAndView;
    }


}
